//
//  Colors.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/18.
//

import UIKit

// MARK:- 颜色相关
public extension UIColor {
    static func color(_ color: CTColors, _ alpha: CGFloat = 1) -> UIColor {
        return UIColor.rgb(color.rgbValue, alpha)
    }
}

public enum CTColors {
    case white
    case black
    ///  主题色
    case theme
    ///  背景色
    case viewBg
    ///  占位色
    case placeholder
    ///  分割线
    case separateLine
    /// nav
    case navBg
    case navTitle
    ///  主标题
    case primary
    ///  二级标题
    case secondary
    ///  三级标题 - 0x999999
    case tertiary
    case hudBg
    case hudTitle
    case toastBg
    case toastTitel
    ///  网址链接色
    case link
}

extension CTColors {
    public var rgbValue: Int {
        switch self {
        case .white:
            return 0xFFFFFF
        case .black:
            return 0x000000
        case .theme:
            return 0xFB6807
        case .viewBg:
            return 0xFAFAFA
        case .placeholder:
            return 0xF9F9F9
        case .separateLine:
            return 0xEBEBEB
        case .navBg:
            return 0xFFFFFF
        case .navTitle:
            return 0x333333
        case .primary:
            return 0x333333
        case .secondary:
            return 0x666666
        case .tertiary:
            return 0x999999
        case .hudBg:
            return 0xDDDDDD
        case .hudTitle:
            return 0x505050
        case .toastBg:
        return 0x111111
        case .toastTitel:
            return 0xFFFFFF
        case .link:
            return 0x313CFF
        }
    }
}

// MARK:- 自定义设置色值
public func kColor(_ RGB: Int, _ alpha: CGFloat = 1) -> UIColor {
    return UIColor.rgb(RGB, alpha)
}

public extension UIColor {
    static func rgb(_ RGB: Int, _ alpha: CGFloat = 1) -> UIColor {
        assert(0...0xFFFFFF ~= RGB, "The color hex value must between 0 to 0xFFFFFF.")
        
        let red = Float(((RGB & 0xFF0000) >> 16)) / 255.0
        let green = Float(((RGB & 0x00FF00) >> 8)) / 255.0
        let blue = Float(RGB & 0x0000FF) / 255.0
        return UIColor.init(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: alpha)
    }
    
    var half: UIColor {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
        getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        return UIColor(red: red, green: green, blue: blue, alpha: alpha * 0.5)
    }
}
